package redis

import (
	"time"
)

type RedisConfig interface {
	GetPassword() string
	GetAddr() string
	GetPoolNum() int
	GetReadTimeout() time.Duration
	GetWriteTimeout() time.Duration
	GetPoolTimeout() time.Duration
	GetDialTimeout() time.Duration
	GetReadOnly() bool
	GetRouteByLatency() bool
	GetRouteRandomly() bool
	GetDb() int
}

//RedisConf redis配置
type RedisConf struct {
	Servers        string `yaml:"addr,flow" ini:"addr"`
	Timeout        string `yaml:"timeout,flow" ini:"timeout"`
	PoolNum        int    `yaml:"pool_num" ini:"pool_num"`
	PoolTimeout    string `yaml:"pool_timeout,flow" ini:"pool_timeout"`
	DialTimeout    string `yaml:"dial_timeout,flow" ini:"dial_timeout"`
	Password       string `yaml:"password,flow" ini:"password"`
	ReadOnly       bool   `yaml:"read_only,flow" ini:"read_only"`
	RouteByLatency bool   `yaml:"route_by_latency,flow" ini:"route_by_latency"`
	RouteRandomly  bool   `yaml:"route_randomly,flow" ini:"route_randomly"`
	Db             int    `yaml:"db,flow" ini:"db"`
}

func (r RedisConf) GetDb() int {
	return r.Db
}

func (r RedisConf) GetPassword() string {
	return r.Password
}

func (r RedisConf) GetAddr() string {
	return r.Servers
}

func (r RedisConf) GetPoolNum() int {
	return r.PoolNum
}

func (r RedisConf) GetReadTimeout() time.Duration {
	timout, err := time.ParseDuration(r.Timeout)
	if err != nil {
		return 0
	}
	return timout
}

func (r RedisConf) GetWriteTimeout() time.Duration {
	timout, err := time.ParseDuration(r.Timeout)
	if err != nil {
		return 0
	}
	return timout
}

func (r RedisConf) GetPoolTimeout() time.Duration {
	timout, err := time.ParseDuration(r.PoolTimeout)
	if err != nil {
		return 0
	}
	return timout
}

func (r RedisConf) GetDialTimeout() time.Duration {
	timout, err := time.ParseDuration(r.DialTimeout)
	if err != nil {
		return 0
	}
	return timout
}

func (r RedisConf) GetReadOnly() bool {
	return r.ReadOnly
}

func (r RedisConf) GetRouteByLatency() bool {
	return r.RouteByLatency
}

func (r RedisConf) GetRouteRandomly() bool {
	return r.RouteRandomly
}
